/*
 * Function test of qtsm_lib_api using glib framework
 *
 * Copyright (c) Huawei Technologies Co., Ltd. 2022. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */
#ifndef QTSM_LIB_GTEST_COMM_H
#define QTSM_LIB_GTEST_COMM_H

#include <glib.h>
#include <stdint.h>
#include <string.h>
#include "qtsm_lib.h"

/* Currently qtsm-server not support 256/512 bit */
#define PCR_MAX_LEN_384         48
#define OPENSSL_OK              1

#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))

enum req_type {
    DESCRIBE_PCR = 0,
    EXTEND_PCR = 1,
    LOCK_PCR = 2,
    LOCK_PCRS = 3,
    DESCRIBE_QTSM = 4,
    ATTESTATION = 5,
    ERROR_CODE = 6,
};
typedef enum req_type req_type;

struct req_describe_pcr {
    uint16_t index;
    uint32_t data_len;
};
typedef struct req_describe_pcr req_describe_pcr;

struct res_describe_pcr {
    bool lock;
    uint32_t data_len;
    uint8_t *data;
};
typedef struct res_describe_pcr res_describe_pcr;

struct req_extend_pcr {
    uint16_t index;
    uint32_t req_data_len;
    uint8_t *req_data;
    uint32_t res_data_len;
};
typedef struct req_extend_pcr req_extend_pcr;

struct res_extend_pcr {
    uint32_t res_data_len;
    uint8_t *res_data;
};
typedef struct res_extend_pcr res_extend_pcr;

struct req_lock_pcr {
    uint16_t index;
};
typedef struct req_lock_pcr req_lock_pcr;

struct res_lock_pcr {
    bool locked;
};
typedef struct res_lock_pcr res_lock_pcr;

struct req_lock_pcrs {
    uint32_t count;
    uint16_t *range;
};
typedef struct req_lock_pcrs req_lock_pcrs;

struct res_lock_pcrs {
    bool locked;
};
typedef struct res_lock_pcrs res_lock_pcrs;

struct req_attestation {
    uint32_t user_data_len;
    uint8_t *user_data;
    uint32_t nonce_len;
    uint8_t *nonce;
    uint32_t pubkey_len;
    uint8_t *pubkey;
};
typedef struct req_attestation req_attestation;

struct qtsm_message_req {
    req_type type;
    union {
        struct req_describe_pcr describe_pcr;
        struct req_extend_pcr extend_pcr;
        struct req_lock_pcr lock_pcr;
        struct req_lock_pcrs lock_pcrs;
        struct req_attestation attestation;
    } qtsm_message_req_u;
};
typedef struct qtsm_message_req qtsm_message_req;

struct qtsm_message_res {
    req_type type;
    union {
        struct res_describe_pcr describe_pcr;
        struct res_extend_pcr extend_pcr;
        struct res_lock_pcr lock_pcr;
        struct res_lock_pcrs lock_pcrs;
        struct describe_qtsm describe_qtsm;
        struct attestation_document attestation;
        enum error_code error_num;
    } qtsm_message_res_u;
};
typedef struct qtsm_message_res qtsm_message_res;

typedef struct comm_req {
    struct qtsm_message_req reqmsg;
    struct qtsm_message_res resmsg;
} comm_req;

void gtest_qtsm_describe_pcr(const void *qtsm_dev_fd);
void gtest_qtsm_extend_pcr(const void *qtsm_dev_fd);
void gtest_qtsm_lock_pcr(const void *qtsm_dev_fd);
void gtest_qtsm_lock_pcrs(const void *qtsm_dev_fd);
void gtest_qtsm_get_describe(const void *qtsm_dev_fd);
void gtest_qtsm_get_attestation(const void *qtsm_dev_fd);

#endif